package com.gameLogic.scene
{
	import com.gameLogic.utils.GameConfig;
	
	import laya.display.Sprite;
	import laya.maths.Point;
	import laya.maths.Rectangle;
	
	public class Camera2D extends Sprite
	{
		
		private var _cameraView:Rectangle;
		public function get cameraView():Rectangle
		{
			return _cameraView;
		}
		
		private var _scene:Sprite;
		
		private var _ease:Number = 0.0025;
		
		private var _focusTarget:Sprite = null;
		
		public function Camera2D(scene:Sprite)
		{
			_cameraView = new Rectangle(0, 0, GameConfig.DeviceW, GameConfig.DeviceH);
			_scene = scene;
			_scene.scrollRect = _cameraView;
		}
		
		/**
		 * 镜头跟随
		 */ 
		public function focus(target:Sprite):void
		{
			_focusTarget = target;
			_cameraView.x = (_focusTarget.x-(GameConfig.DeviceW>>1));
			_cameraView.y = (_focusTarget.y-(GameConfig.DeviceH>>1));
		}
		
		private var _pos:Point = new Point();
		public function update():void
		{
			if(_focusTarget)
			{
				_pos.x = (_focusTarget.x-(GameConfig.DeviceW>>1));
				_pos.y = (_focusTarget.y-(GameConfig.DeviceH>>1));
				if(_pos.x + GameConfig.DeviceW <= WorldMap.instance.gridVO.gridW && _pos.x >= 0)
				{
					_cameraView.x += (_pos.x - _cameraView.x) * Laya.timer.delta * _ease;
				}
				if(_pos.y + GameConfig.DeviceH <= WorldMap.instance.gridVO.gridH && _pos.y >= 0)
				{
					_cameraView.y += (_pos.y - _cameraView.y) * Laya.timer.delta * _ease;
				}
			}
		}
		
	}
}

